﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SmartMathLibrary
{
    /// <summary>
    /// This class provides the interpolation of a complex polynomial by only complex points.
    /// </summary>
    [Serializable]
    public class ComplexPolynomialInterpolation
    {
        private readonly ComplexNumber[] xValues;
        private readonly ComplexNumber[] yValues;

        /// <summary>
        /// Initializes a new instance of the <see cref="ComplexPolynomialInterpolation"/> class.
        /// </summary>
        /// <param name="xValues">The list of x values, which defines the polynomial.</param>
        /// <param name="yValues">The list of y values, which defines the polynomial.</param>
        public ComplexPolynomialInterpolation(ComplexNumber[] xValues, ComplexNumber[] yValues)
        {
            if (xValues.Length != yValues.Length)
            {
                throw new Exception("The number of x values has to be even with the number of y values");
            }

            this.xValues = xValues;
            this.yValues = yValues;
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="ComplexPolynomialInterpolation"/> class.
        /// </summary>
        /// <param name="list">The list of points, which defines the polynomial.</param>
        public ComplexPolynomialInterpolation(ComplexPoint2D[] list)
        {
            this.xValues = new ComplexNumber[list.Length];
            this.yValues = new ComplexNumber[list.Length];

            for (int i = 0; i < list.Length; i++)
            {
                this.xValues[i] = list[i].X;
                this.yValues[i] = list[i].Y;
            }
        }

        /// <summary>
        /// Gets the x values for the interpolation.
        /// </summary>
        /// <value>The x values for the interpolation.</value>
        public ComplexNumber[] XValues
        {
            get { return xValues; }
        }

        /// <summary>
        /// Gets the y values for the interpolation.
        /// </summary>
        /// <value>The y values for the interpolation.</value>
        public ComplexNumber[] YValues
        {
            get { return yValues; }
        }

        /// <summary>
        /// Interpolates the polynomial which is itemized by the specified x and y values.
        /// </summary>
        /// <returns>The interpolated complex polynomial.</returns>
        public ComplexPolynomial InterpolatePolynomial()
        {
            ComplexLagrangePolynomial lagrangePolynomial = new ComplexLagrangePolynomial(this.xValues, this.yValues);

            return lagrangePolynomial.ToPolynomial();
        }
    }
}